package game;

import game.manager.CalendarManager;
import game.manager.EnergyManager;
import game.manager.EventManager;
import game.manager.InputManager;
import game.manager.Manager;
import game.manager.ConversationManager;
import game.manager.PersonManager;
import game.manager.RelationshipManager;
import game.manager.ScoreManager;
import game.manager.ScreenManager;

import java.util.Collection;
import java.util.Vector;

/**
 * Starting point of the game
 * 
 * @author churchmf
 * 
 */
public class Game implements Runnable {
	private Thread thread;
	private Collection<Manager> managers;

	public static void main(String[] args) {
		final Game game = new Game();
		game.initialize();
	}

	private void initialize() {
		this.managers = new Vector<Manager>(this.getManagers());
		this.registerEventManagerObservers(managers);

		thread = new Thread(this);
		thread.start();
	}

	private void registerEventManagerObservers(Collection<Manager> managers) {
		for (Manager manager : managers)
			if (!(manager instanceof EventManager))
				EventManager.getManager().addObserver(manager);
	}

	private Collection<Manager> getManagers() {
		Vector<Manager> managers = new Vector<Manager>();
		managers.add(CalendarManager.getManager());
		managers.add(PersonManager.getManager());
		managers.add(RelationshipManager.getManager());
		managers.add(ConversationManager.getManager());
		managers.add(InputManager.getManager());
		managers.add(ScreenManager.getManager());
		managers.add(EventManager.getManager());
		managers.add(ScoreManager.getManager());
		managers.add(EnergyManager.getManager());
		return managers;
	}

	@Override
	public void run() {
		while (true) {
			for (Manager manager : managers) {
				manager.gameUpdate();
			}
			try {
				Thread.sleep(1000 / 30);
			} catch (InterruptedException e) {
			}
		}
	}
}
